package com.tensquare.gathering.controller;

import com.tensquare.gathering.pojo.Gathering;
import com.tensquare.gathering.service.GatheringService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/26 10:05
 */
@RestController
@RequestMapping("/gathering")
@CrossOrigin
public class GatheringController {

    @Autowired
    private GatheringService gatheringService;

    /**
     * fetch data by rule id
     *
     * @description  增加活动
     * @param
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Gathering gathering){
        gatheringService.add(gathering);
        return new Result(true, StatusCode.OK,"增加成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  活动全部列表
     * @param
     * @return
     */
    @GetMapping
    public Result findAll(){
        return new Result(true,StatusCode.OK,"查询成功",gatheringService.findAll());
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID查询活动
     * @param
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",gatheringService.findById(id));
    }

    /**
     * fetch data by rule id
     *
     * @description  修改活动
     * @param
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable String id,@RequestBody Gathering gathering){
        gathering.setId(id);
        gatheringService.update(gathering);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  删除活动
     * @param
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        gatheringService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据条件查询活动列表
     * @param
     * @return
     */
    @PostMapping("/search")
    public Result findSearch(@RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",gatheringService.findSearch(searchMap));
    }

    /**
     * fetch data by rule id
     *
     * @description  活动分页
     * @param
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result findSearch(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size){
        Page<Gathering> pageList = gatheringService.findSearch(searchMap, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }

    /**
     * fetch data by rule id
     *
     * @description  根据城市显示分页内容
     * @param
     * @return
     */
    @GetMapping("/city/{city}/{page}/{size}")
    public Result findListByCity(@PathVariable String city,@PathVariable int page,@PathVariable int size){
        Page<Gathering> pageList = gatheringService.findListByCity(city, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }

}
